package work.atpgroup.service.impl;

import org.springframework.stereotype.Service;
import work.atpgroup.entity.ResourceLink;
import work.atpgroup.entity.tool.ApplicationProperties;
import work.atpgroup.mapper.ResourceLinkMapper;
import work.atpgroup.service.ResourceService;

import javax.annotation.Resource;
import java.util.List;

@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {
    @Resource(name="resourceLinkMapper")
    private ResourceLinkMapper resourceLinkMapper;

    @Override
    public int insertResourceLink(ResourceLink resourceLink) {
        return resourceLinkMapper.insertResourceLink(resourceLink);
    }

    @Override
    public int updateResourceLink(ResourceLink resourceLink) {
        return resourceLinkMapper.updateResourceLink(resourceLink);
    }

    @Override
    public int deleteResourceLinkById(Integer id) {
        return resourceLinkMapper.deleteResourceLinkById(id);
    }

    @Override
    public int deleteResourceLinkByUserId(Integer userId) {
        return resourceLinkMapper.deleteResourceLinkByUserId(userId);
    }

    @Override
    public String getResourceLinkPathById(Integer id) {
        return resourceLinkMapper.getResourceLinkPathById(id);
    }

    @Override
    public List<String> getResourceLinkPathByUserId(Integer userId) {
        return resourceLinkMapper.getResourceLinkPathByUserId(userId);
    }

    @Override
    public List<String> getResourceLinkPathByUserIdAndDateAsc(Integer userId) {
        return resourceLinkMapper.getResourceLinkPathByUserIdAndDateAsc(userId);
    }

    @Override
    public List<String> getResourceLinkPathByUserIdAndDateDesc(Integer userId) {
        return resourceLinkMapper.getResourceLinkPathByUserIdAndDateDesc(userId);
    }

    @Override
    public int getResourceLinkIdByPath(String path) {
        return resourceLinkMapper.getResourceLinkIdByPath(path);
    }

    /***
     * 获取服务器用存储资源的地址
     * **/
    @Override
    public String getApplicationPath(){
        return ApplicationProperties.server_static_path;
    }
    /**
     * 把相对地址转化为绝对地址,相对地址来源： 直接相对地址，经过数据库查找，从资源id映射的相对路径
     * **/
    @Override
    public String getPrivateResourceAbsolutePath(String resourcePath){
        return ApplicationProperties.getPrivateResourceAbsolutePath(resourcePath);
    }
    /**
     * 为避免被 ddos攻击，资源链尽可以被用户自身访问，其他用户访问公有链。
     * 把相对地址转换为 无需权限 的公有地址
     * **/
    @Override
    public String getPublicUriPath(String resourcePath) {
        return ApplicationProperties.getPublicUriPath(resourcePath);
    }

    @Override
    public String getPublicUriByResourceId(Integer resourceId) {
        return getPublicUriPath(resourceLinkMapper.getResourceLinkPathById(resourceId));
    }

    @Override
    public ResourceLink getResourceLinkById(Integer id) {
        return resourceLinkMapper.getResourceLinkById(id);
    }

    @Override
    public ResourceLink getResourceLinkByPath(String resourcepath) {
        return resourceLinkMapper.getResourceLinkByPath(resourcepath);
    }

    @Override
    public List<ResourceLink> getResourceLinkByUserId(Integer userId) {
        return resourceLinkMapper.getResourceLinkByUserId(userId);
    }

    @Override
    public List<ResourceLink> getResourceLinkByUserIdAndDateAsc(Integer userId) {
        return resourceLinkMapper.getResourceLinkByUserIdAndDateAsc(userId);
    }

    @Override
    public List<ResourceLink> getResourceLinkByUserIdAndDateDesc(Integer userId) {
        return resourceLinkMapper.getResourceLinkByUserIdAndDateDesc(userId);
    }

    @Override
    public Integer getUserIdByResourceId(Integer id) {
        return resourceLinkMapper.getUserIdByResourceId(id);
    }
}
